

package com.xiuluo.dubbotest.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.toolkit.MapUtils;
import com.xiuluo.dubbotest.common.utils.RedisKeyUtil;
import com.xiuluo.dubbotest.model.dto.DubboInvokeReqRecordDTO;
import com.xiuluo.dubbotest.model.dto.DubboInvokeRespRecordDTO;
import com.xiuluo.dubbotest.service.CacheService;
import com.xiuluo.dubbotest.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import com.xiuluo.dubbotest.common.wrapper.PageResult;
import com.xiuluo.dubbotest.model.dto.ResponseDTO;
import com.xiuluo.dubbotest.model.dto.ResponseUtil;
import com.xiuluo.dubbotest.model.po.UserPO;
import com.xiuluo.dubbotest.service.DubboInvokeReqRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author idea
 * @version V1.0
 * @date 2020/3/1
 */
@RestController
@Slf4j
@RequestMapping(value = "/dubbo-invoke-req-record")
public class DubboInvokeReqRecordController {

    @Autowired
    private DubboInvokeReqRecordService dubboInvokeReqRecordService;

    @Resource
    private RedisService redisService;

    @Autowired
    private CacheService cacheService;


    @PostMapping(value = "/select-in-page")
    public ResponseDTO<PageResult<DubboInvokeRespRecordDTO>> selectInPage(@RequestBody DubboInvokeReqRecordDTO dubboInvokeReqRecordDTO) {
        log.info("【selectInPage】 req args is {}", dubboInvokeReqRecordDTO);
        String token = RedisKeyUtil.buildToken(dubboInvokeReqRecordDTO.getUserToken());
//        UserPO userPO = redisService.getObject(token, UserPO.class);
        UserPO userPO = cacheService.getCache(token, UserPO.class);
        if (userPO == null) {
            return ResponseUtil.unauthorized();
        }
        PageResult<DubboInvokeRespRecordDTO> resultList = dubboInvokeReqRecordService.selectDubboInokeParam(userPO.getId(), dubboInvokeReqRecordDTO.getPage(), dubboInvokeReqRecordDTO.getPageSize());
        return ResponseUtil.successResponse(resultList);
    }

    @PostMapping(value = "/save-one")
    public ResponseDTO<Boolean> saveOne(@RequestBody DubboInvokeReqRecordDTO dubboInvokeReqRecordDTO) {
        log.info("【saveOne】 req args is {}", dubboInvokeReqRecordDTO);
        String token = RedisKeyUtil.buildToken(dubboInvokeReqRecordDTO.getUserToken());
//        UserPO userPO = redisService.getObject(token, UserPO.class);
        UserPO userPO = cacheService.getCache(token, UserPO.class);
        if (userPO == null) {
            return ResponseUtil.unauthorized();
        } else {
            dubboInvokeReqRecordDTO.setUserId(userPO.getId());
        }
        Boolean result = dubboInvokeReqRecordService.saveOne(dubboInvokeReqRecordDTO);
        return ResponseUtil.successResponse(result);
    }

    @PostMapping(value = "/delete-one")
    public ResponseDTO<Boolean> deleteOne(@RequestBody Map<String,Object> parmas) {
        log.info("【deleteOne】 req args is {}", JSON.toJSONString(parmas));
        String userToken = (String) parmas.get("userToken");
        Integer id = (Integer) parmas.get("id");
        String token = RedisKeyUtil.buildToken(userToken);
//        UserPO userPO = redisService.getObject(token, UserPO.class);
        UserPO userPO = cacheService.getCache(token, UserPO.class);
        if (userPO == null) {
            return ResponseUtil.unauthorized();
        } else {
            dubboInvokeReqRecordService.deleteOne(id,userPO.getId());
            return ResponseUtil.successResponse();
        }
    }

}
